package com.ruicar.afs.cloud.apply.pre.loan.mq.receiver;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.apply.business.mq.config.ArchiveConfig;
import com.ruicar.afs.cloud.apply.business.service.ArchiveMQService;
import com.ruicar.afs.cloud.apply.common.entity.*;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.contract.service.ApplyContractInfoService;
import com.ruicar.afs.cloud.apply.pre.loan.mq.dto.ContractMainInfoDTO;
import com.ruicar.afs.cloud.apply.pre.loan.mq.sender.CaseLoanInfoSender;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyOrderInfoService;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;


/**
 * @author panhj
 */
@AllArgsConstructor
@Slf4j
@Component
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class ApplyContractProcessor implements AfsMqBizProcessor<ContractMainInfoDTO> {

    /**
     * 合同信息service
     */
    private final ApplyContractInfoService applyContractInfoService;
    private final CaseLoanInfoSender caseLoanInfoSender;
    private final ComAttachmentFileService comAttachmentFileService;
    private final ArchiveMQService archiveMQService;
    private final ApplyOrderInfoService applyOrderInfoService;


    /**
     * @return
     */
    @Override
    public MqTransCode getCode() {
        return MqTransCode.CONTRACT_BASIC_POS_APPLY;
    }
    /**
     * @param contractMainInfoDTO afs trans entity
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean processMessage(ContractMainInfoDTO contractMainInfoDTO) throws Exception {
        if (contractMainInfoDTO!= null) {
                this.applyRemindDetails(contractMainInfoDTO);
        }
        return true;
    }

    /**
     * 解析合同信息报文,保存数据
     *
     * @param contractMainInfoDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public void applyRemindDetails(ContractMainInfoDTO contractMainInfoDTO) {
        if (ObjectUtils.isEmpty(contractMainInfoDTO)) {
            throw new AfsBaseException("合同编号:" + contractMainInfoDTO.getContractNo() + ",contractMainInfoDTO");
        }
        if (ObjectUtils.isEmpty(contractMainInfoDTO.getApplyNo())) {
            throw new AfsBaseException("合同编号:" + contractMainInfoDTO.getContractNo() + "，合同编号不能为空");
        }
        //留言表
        ApplyContractInfo applyContractInfo =applyContractInfoService.getOne(Wrappers.<ApplyContractInfo>query().lambda()
        .eq(ApplyContractInfo::getContractNo,contractMainInfoDTO.getContractNo()));
        if(contractMainInfoDTO.getContractStatus().equals(ApplyConstants.CONTRACT_CANCEL)){
            applyContractInfo.setCancelDate(contractMainInfoDTO.getCancelDate());
            applyContractInfo.setContractStatus(ApplyConstants.CONTRACT_STATUS_CANCEL);
            applyContractInfo.setContractId(contractMainInfoDTO.getId().toString());
            this.applyContractInfoService.updateById(applyContractInfo);
        }else{
            applyContractInfo.setLoanDate(contractMainInfoDTO.getLoanDate());
            applyContractInfo.setStartDate(contractMainInfoDTO.getStartDate());
            applyContractInfo.setEndDate(contractMainInfoDTO.getEndDate());
            applyContractInfo.setContractId(contractMainInfoDTO.getId().toString());
            this.applyContractInfoService.updateById(applyContractInfo);
            ApplyOrderInfo applyOrderInfo=applyOrderInfoService.getOrderInfoByApplyNo(applyContractInfo.getApplyNo());
            applyOrderInfo.setCancelDate(new Date());
            applyOrderInfo.setApplyStatus(ApplyConstants.APPLY_STATUS_CANCEL);
            applyOrderInfoService.updateById(applyOrderInfo);
            archiveMQService.archivePushMQ(applyContractInfo.getId().toString(), applyContractInfo.getContractNo(), applyContractInfo.getApplyNo(), ArchiveConfig.wwcontract_actived);
            AfsTransEntity<List<ComAttachmentFile>> listAfsTransEntity = this.listAfsTransEntity(contractMainInfoDTO.getContractNo());
            log.info("********推送合同系统模板数据*******"+listAfsTransEntity);
            caseLoanInfoSender.sendFileToContract(listAfsTransEntity);
        }
    }


    public AfsTransEntity<List<ComAttachmentFile>> listAfsTransEntity(String contractNo) {
        AfsTransEntity<List<ComAttachmentFile>> transEntity = new AfsTransEntity<>();
        List<ComAttachmentFile> list=comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda().eq(ComAttachmentFile::getBusiNo,contractNo).eq(ComAttachmentFile::getFileSource, ApplyConstants.PRINT_FILE));
        transEntity.setTransCode(MqTransCode.WOW_APPLY_MANAGE_ICOS_CONTRACT_PDF);
        transEntity.setData(list);
        return transEntity;
    }
}
